package com.imooc.controller;

import com.imooc.common.utils.StringUtils;
import com.imooc.pojo.BO.AddressBO;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lihongyu
 */
@RestController
@RequestMapping("/address")
@Api(value="地址接口",tags = "地址相关接口")
public class AddressController extends BaseController{

    @Resource
    private AddressService addressService;

    /**
     * 获取收货地址列表
     *
     * @param userId 用户Id
     * @return 用户收货地址列表
     */
    @RequestMapping("list")
    @ApiOperation(value = "获取收货地址列表", notes = "获取收货地址列表", httpMethod = "POST")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    public IMOOCJSONResult queryAddressList(@RequestParam String userId){
        if(StringUtils.isEmpty(userId)){
            return IMOOCJSONResult.errorMsg("");
        }
        List<UserAddress> userAddressList = addressService.queryAllAddressList(userId);
        return IMOOCJSONResult.ok(userAddressList);
    }

    /**
     * 新增用户收货地址
     *
     * @param addressBO 添加收货地址参数封装实体
     * @return boolean 成功/失败
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增收货地址", notes = "新增收货地址", httpMethod = "POST")
    public IMOOCJSONResult addUserAddress(@RequestBody AddressBO addressBO){
        boolean result = addressService.addUserAddress(addressBO);
        return IMOOCJSONResult.ok(result);
    }

    /**
     * 修改用户收货地址
     *
     * @param addressBO 修改收货地址参数封装实体
     * @return boolean 成功/失败
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", httpMethod = "POST")
    public IMOOCJSONResult updateUserAddress(@RequestBody AddressBO addressBO){
        boolean result = addressService.updateUserAddress(addressBO);
        return IMOOCJSONResult.ok(result);
    }

    /**
     * 删除用户收货地址
     *
     * @param userId 用户Id
     * @param addressId 地址Id
     * @return boolean 成功/失败
     */
    @PostMapping("/delete")
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",paramType = "query"),
            @ApiImplicitParam(name = "addressId",value = "地址Id",paramType = "query")
    })
    public IMOOCJSONResult deleteUserAddress(@RequestParam String userId,@RequestParam String addressId){
        if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(addressId)){
            return IMOOCJSONResult.errorMap("");
        }
        boolean result = addressService.deleteUserAddress(userId,addressId);
        return IMOOCJSONResult.ok(result);
    }

    /**
     * 设置收货地址为默认地址
     *
     * @param userId 用户Id
     * @param addressId 地址Id
     * @return boolean 成功/失败
     */
    @PostMapping("/setDefalut")
    @ApiOperation(value = "设置收货地址为默认地址", notes = "设置收货地址为默认地址", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户Id",paramType = "query"),
            @ApiImplicitParam(name = "addressId",value = "地址Id",paramType = "query")
    })
    public IMOOCJSONResult setDefaultUserAddress(@RequestParam String userId,@RequestParam String addressId){
        if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(addressId)){
            return IMOOCJSONResult.errorMap("");
        }
        boolean result = addressService.setDefaultUserAddress(userId,addressId);
        return IMOOCJSONResult.ok(result);
    }


}
